/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/09/09
* Description        : Main program body.
*******************************************************************************/
#include "ch32v30x.h"
#include <rtthread.h>
#include <rthw.h>
#include "drivers/pin.h"
#include <board.h>
#include "at.h"
#include "drivers/serial.h"
#include "bc26_mqtt.h"
/* Global typedef */

/* Global define */


#define LED0_PIN  35   //PC3

/* Global Variable */
int test_at(void);
void soild_sensor(void);
 at_response_t resp = RT_NULL;
 at_client_t client = RT_NULL;
int main(void)
{
    rt_device_t serial = rt_device_find("uart2");
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

//    config.baud_rate = 115200;
//    config.data_bits = DATA_BITS_8;
//    config.stop_bits = STOP_BITS_1;
//    config.bufsz     = AT_CLIENT_RECV_BUFF_LEN;
//    config.parity    = PARITY_NONE;

    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    rt_device_close(serial);

    at_client_init("uart2", 512);
    client = at_client_get("uart2");


    rt_kprintf("MCU: CH32V307\n");
	rt_kprintf("SysClk: %dHz\n",SystemCoreClock);
    rt_kprintf("www.wch.cn\n");
	LED1_BLINK_INIT();

	rt_thread_t thread = rt_thread_create("at init", (void*)test_at, (void*)0, 512, 3, 10);
	rt_thread_startup(thread);
	thread = rt_thread_create("soild sensor", (void*)soild_sensor, (void*)0, 512, 10, 10);
	rt_thread_startup(thread);

	GPIO_ResetBits(GPIOA,GPIO_Pin_0);
	while(1)
	{
	    GPIO_SetBits(GPIOA,GPIO_Pin_0);
	    rt_thread_mdelay(500);
	    GPIO_ResetBits(GPIOA,GPIO_Pin_0);
	    rt_thread_mdelay(500);
	}
}

int test_at(void)
{
    char *text = RT_NULL;
    //rt_pin_mode();
    at_client_obj_wait_connect(client, 5000);
    rt_kprintf("AT OK!\r\n");

    resp = at_create_resp(256, 0, 5000);
    while(RT_EOK != at_obj_exec_cmd(client, resp, "AT+CFUN=1"))     {rt_kprintf("Err CFUN");at_delete_resp(resp);resp = at_create_resp(256, 0, 5000);}
    at_delete_resp(resp);

    resp = at_create_resp(256, 0, 5000);
    while(RT_EOK != at_obj_exec_cmd(client, resp, "AT+QICLOSE=0"))  {rt_kprintf("Err QICLOSE");at_delete_resp(resp);resp = at_create_resp(256, 0, 5000);}
    at_delete_resp(resp);

    //卡号
    resp = at_create_resp(256, 4, 5000);
    if(at_obj_exec_cmd(client, resp, "AT+CIMI") != RT_EOK)
    {
        rt_kprintf("AT+CIMI Err!\r\n");
        return -1;
    }
    rt_kprintf("AT+CIMI OK!\r\n");
    text = (char*)at_resp_get_line_by_kw(resp,"460");
    if(RT_NULL == text)
    {
        at_delete_resp(resp);
        rt_kprintf("NBCard Err!\r\n");
        return -1;
    }
    rt_kprintf("NBC %s\r\n",text);
    rt_kprintf("NBCard Code OK!\r\n");

    resp = at_create_resp(256, 0, 5000);
    while(RT_EOK != at_obj_exec_cmd(client, resp, "AT+CGATT=1"))  {rt_kprintf("Err CGATT");at_delete_resp(resp);resp = at_create_resp(256, 0, 5000);}
    at_delete_resp(resp);

    CGATT:  //注册
    resp = at_create_resp(256, 4, 5000);
    if(at_obj_exec_cmd(client, resp, "AT+CGATT?") != RT_EOK)
    {
        rt_kprintf("AT+CGATT Err!\r\n");
        return -1;
    }
    rt_kprintf("AT+CGATT OK!\r\n");
    text = (char*)at_resp_get_line_by_kw(resp,"+CGATT: 1");
    if(RT_NULL == text)
    {
        at_delete_resp(resp);
        rt_kprintf("AT+CGATT Code Err!\r\n");
        goto CGATT;
    }
    rt_kprintf("Ret %s\r\n",text);
    rt_kprintf("AT+CGATT Code OK!\r\n");
    at_delete_resp(resp);

    CSQ://信号强度
    resp = at_create_resp(256, 4, 5000);
    if(at_obj_exec_cmd(client, resp, "AT+CSQ") != RT_EOK)
    {
        rt_kprintf("AT+CSQ Err!\r\n");
        return -1;
    }
    rt_kprintf("AT+CSQ OK!\r\n");
//    text = (char*)at_resp_get_line_by_kw(resp,"+CSQ: 1");
//    if(RT_NULL == text)
//    {
//        at_delete_resp(resp);
//        rt_kprintf("AT+CSQ Code Err!\r\n");
//        goto CSQ;
//    }
//    rt_kprintf("line %s\r\n",text);
//    rt_kprintf("AT+CSQ Code ok!\r\n");
//    at_delete_resp(resp);






    //while(1) rt_thread_mdelay(1000);
}

int led(void)
{
    rt_uint8_t count;

    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
    //rt_kprintf("led_SP:%08x\r\n",__get_SP());
    resp = at_create_resp(256, 3, 5000);
    if(at_obj_exec_cmd(client, resp, "AT") == RT_EOK)
    {
        rt_kprintf("AT OK!\r\n");
        char *temp = (char*)at_resp_get_line(resp,1);
        if(NULL == temp)
            rt_kprintf("None\r\n");
        else
        {
            //char *t = "i am gold\0";
            rt_kprintf("%s",temp);
            rt_kprintf("\r\n");
        }
    }
    else
    {
        rt_kprintf("AT Err\r\n");
    }
    for(count = 0 ; count < 2 ;count++)
    {
        rt_pin_write(LED0_PIN, PIN_LOW);
        rt_kprintf("led on, count : %d\r\n", count);
        rt_thread_mdelay(500);

        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_kprintf("led off\r\n");
        rt_thread_mdelay(500);
    }
    at_delete_resp(resp);
    return 0;
}

void soild_sensor(void)
{
    rt_adc_device_t adc_dev;
    rt_uint32_t value, vol;
    rt_err_t ret = RT_EOK;
    /* 查 找 设 备 */
    adc_dev = (rt_adc_device_t)rt_device_find("adc1");
    if (adc_dev == RT_NULL)
    {
        rt_kprintf("can't find adc1 device!\n");
        return RT_ERROR;
    }
    /* 使 能 设 备 */
    ret = rt_adc_enable(adc_dev, 1);
    /* 读 取 采 样 值 */
    while(1)
    {
        value = rt_adc_read(adc_dev, 1);
        rt_kprintf("the value is :%d \n", value);
        /* 转 换 为 对 应 电 压 值 */
        vol = value * 330 / (1<<12);
        rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
        rt_thread_mdelay(5000);
    }
    /* 关 闭 通 道 */
    ret = rt_adc_disable(adc_dev, 1);
    return ;
}
int bc26_sub(void)
{
    /* 订 阅 消 息 */
    return RT_EOK;
}
int bc26_publish(int temp, int humi, int soild)
{
    /* 发 布 消 息*/
    resp = at_create_resp(256, 0, 5000);
    while(RT_EOK != at_obj_exec_cmd(client, resp, "AT+QMTPUB=0,0,0,0,\"/sys/h6fsIcWC3Ke/test001/thing/event/property/post\",\"{\"id\":\"26\",\"version\":\"1.0\",\"params\":{\"CurrentTemperature\":{\"value\":%d},\"CurrentHumidity\":{\"value\":%d}},\"method\":\"thing.event.property.post\"}\"",temp,humi))
    {   rt_kprintf("Err CFUN");
        at_delete_resp(resp);
        resp = at_create_resp(256, 0, 5000);
    }
    at_delete_resp(resp);
    return RT_EOK;
}

static void urc_sub_func(const char *data, rt_size_t size)
{
    /* 订 阅 信 息 解 析 */
    LOG_D("AT client device sub success!");
    rt_kprintf(data);
}


/* 导 出 到 msh 命 令 列 表 中 */
MSH_CMD_EXPORT(soild_sensor, adc voltage convert sample);

MSH_CMD_EXPORT(led,  led sample by using I/O drivers);

MSH_CMD_EXPORT(test_at,BC26 test);

